//============================================================================
// Name        : sample.cpp
// Author      : 
// Version     :
// Copyright   : MIT
// Description : Sample in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include "../include/cli.h"
#include <string>

using namespace std;

/* 格式输出 */
#define PT_CMLPRE    \
        printf("[Sample] # "); \
        fflush(stdout)

/* omCmdCheck 返回码 */
#define OM_CML_CHECK_OK             0
#define OM_CML_CHECK_ERROR         -1
#define OM_CML_CHECK_NULL_CMD      -2
#define OM_CML_CHECK_NOT_FIND      -3
int omCmdCheck(int argc,char argv[OM_CML_PARSE_NUM_MAX][OM_CML_PARSE_CHAR_MAX]){
	if(argc <= 0)
	{
		return OM_CML_CHECK_NULL_CMD;
	}

	//判断一下名字是否在命令表中
	if(!isInCmdListTable(argv[0])){
		return OM_CML_CHECK_NOT_FIND;
	}

	return OM_CML_CHECK_OK;
}

void omCmlHelp()
{
    printf("\nsupport commands :");
    printf("\n+-----------------+-------------------------------------------------+");
    printf("\n|  Command Name   |               Description                       |");
    printf("\n+-----------------+-------------------------------------------------+");
    printf("\n|  help           |           help info to this cli                 |");
    printf("\n|  show           |           show cli version                      |");
    printf("\n|  shutdown       |           shutdown sample                       |");
    printf("\n|  restart        |           restart sample                        |");
    printf("\n|  mycmd-xxx      |           ready to define                       |");
    printf("\n+-----------------+-------------------------------------------------+");
    printf("\n");

}

void sample_show(){
	printf("  version      :    v1.0 beta, compiled at 2018-8-31\n");
}

void sample_shutdown(){
	// do.. here add your code..
}

void sample_restart(){
	// do.. here add your code..
}

//xclip -o
void sample_ctrlv_proc(char *cb,int len){
	FILE *fp;

	system("xclip -o > ./.clipboardtemp.txt");
	fp = fopen("./clipboardtemp.txt","r");
	if(!fp)return;
	int i=0;
	while(!feof(fp)){
		fscanf(fp,"%c",&cb[i++]);
		if(i == len-1){
			cb[i] = 0;
			fclose(fp);
			printf("clipboard is up to the max cmd len(%d).\n",OM_CML_CHAR_MAX);
			return ;
		}
	}
	cb[i] = 0;
	fclose(fp);
	return ;
}

//sample主函数
int main(int argc0,char * argv0[]) {
	int  rc;
	char cmd[OM_CML_CHAR_MAX];
	char argv[OM_CML_PARSE_NUM_MAX][OM_CML_PARSE_CHAR_MAX]= {0};
	int argc;

	printf("----------------------------------------------------------------------------\n");
	printf("|                                                                          |\n");
	printf("|                      Welcome to Open Cli Sample ^^                       |\n");
	printf("|                 Press help to show all cmds , ctrl+c for exit            |\n");
	printf("|                    try tab and up/down when you using..                  |\n");
	printf("|                                                                          |\n");
	printf("----------------------------------------------------------------------------\n");

	cli_init();
	cli_setCtrlvProcFunc(sample_ctrlv_proc);

	while(1)
	{
		fflush(stdout);

		PT_CMLPRE;

		/* 从命令行输入得到一段命令字符串 */
		getcmd(cmd);

		/* cmdline parse */
		omCmdlineParse(cmd,&argc,argv);

		/*cmd check*/
		rc = omCmdCheck(argc,argv);

		switch(rc)
		{
			case OM_CML_CHECK_NULL_CMD:
				break;
			case OM_CML_CHECK_NOT_FIND:
				printf("command not find.\n");
				break;
			case OM_CML_CHECK_ERROR:
				printf("command error.\n");
				break;
			case OM_CML_CHECK_OK:
				if(strcmp(argv[0],"help") == 0){
					omCmlHelp();
					break;
				}
				if(strcmp(argv[0],"show") == 0){
					sample_show();
					break;
				}

				if(strcmp(argv[0],"shutdown") == 0){
					sample_shutdown();
					break;
				}

				if(strcmp(argv[0],"restart") == 0){
					sample_restart();
					break;
				}	
				
				printf("command not support.\n");
				break;
		}
	}
	return 0;
}
